package ua.pp.bizon.yandex.uploader.atom;

import ua.pp.bizon.yandex.uploader.remote.RemoteAlbumFactory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.abdera.model.Element;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpException;

public class AlbumsList extends Entry implements Iterable<EntryAlbum> {

	public static final String ID_ROOT = "root";
	private String autorName;
	private List<EntryAlbum> albums;

	public AlbumsList(Element e, String autorName, List<EntryAlbum> albums) {
		super(e);
		this.autorName = autorName;
		this.albums = albums;
	}

	@Override
	public String toString() {
		return "AlbumsList [autorName=" + getAutorName() + ", albums=" + getAlbums() + "]";
	}

	/**
	 * 
	 * @return author name for collection of albums
	 */
	public String getAutorName() {
		return autorName;
	}

	/**
	 * 
	 * @param autorName
	 *            set the new author name; in Entry will NOT changed
	 */
	public void setAutorName(String autorName) {
		this.autorName = autorName;
	}

	/**
	 * @return list of known albums for this directory level
	 */
	protected List<EntryAlbum> getAlbums() {
		if (albums == null) {
			albums = new ArrayList<EntryAlbum>();
		}
		return albums;
	}

	/**
	 * 
	 * @param parent
	 *            - link for album that should be found
	 * @return album, if it known
	 * @warning it work recursively !
	 */
	protected EntryAlbum getAlbumByLink(String parent) {
		for (EntryAlbum e : getAlbums()) {
			if (e.getLink(EntryAlbum.SELF).getLink().equals(parent)) {
				return e;
			}
			if (e.getAlbums().size() > 0) {
				EntryAlbum x = e.getAlbumByLink(parent);
				if (x != null)
					return x;
			}
		}
		return null;
	}

	/**
	 * @warning this method don't create the album on server!
	 * @param e
	 *            - new album
	 */
	protected void addAlbum(EntryAlbum e) {
		albums.add(e);
	}

	@Deprecated
	public EntryAlbum findFolder(List<String> pathTo) throws HttpException {
		String folder = pathTo.get(0);
		EntryAlbum to = findFolder(folder);
		if (to == null) {
			return null;
		}
		if (pathTo.size() > 1) {
			return to.findFolder(pathTo.subList(1, pathTo.size()));
		}
		return to;
	}

	public EntryAlbum findFolder(String folderName) {
		EntryAlbum to = null;
		for (EntryAlbum e : getAlbums()) {
			if (e.getName().equals(folderName)) {
				to = e;
			}
		}
		return to;
	}

	@Override
	public Iterator<EntryAlbum> iterator() {
		return getAlbums().iterator();
	}

	public String getName() {
		return autorName;
	}

	public EntryAlbum createAlbum(String foldername) {
		for (int i = 0; i < 3; i++) {
			try {
				EntryAlbum to = remoteCreate(foldername);
				if (to != null)
					return to;
			} catch (HttpException e) {
				LogFactory.getLog(getClass()).warn(e.getMessage(), e);
			}
		}
		return null;
	}

	protected EntryAlbum remoteCreate(String foldername) throws HttpException {
		EntryAlbum album = new RemoteAlbumFactory().createAlbum(foldername);
		getAlbums().add(album);
		return album;
	}

	public EntryAlbum get(int childIndex) {
		return albums.get(childIndex);
	}

	public int indexOf(AlbumsList list) {
		return albums.indexOf(list);
	}

	public int size() {
		return albums.size();
	}

	public AlbumsList findFolderByID(String to) {
		if (to.equals(ID_ROOT)) {
			return this;
		} else {
			for (EntryAlbum e : this) {
				if (to.equals(e.getID())) {
					return e;
				}
				AlbumsList findFolderByID = e.findFolderByID(to);
				if (findFolderByID != null) {
					return findFolderByID;
				}
			}
		}
		return null;
	}

	public void remove(String folderName) throws HttpException {
		EntryAlbum album = findFolder(folderName);
		new RemoteAlbumFactory().removeAlbum(album.getLink(EntryAlbum.SELF));
		getAlbums().remove(album);
	}
}
